package com.mp3s.dc.shell;

import java.io.File;

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

import com.mp3s.dc.service.Config;
import com.mp3s.dc.utils.UUID;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

@ShellComponent
public class DSCommands {

    @ShellMethod(key = "v", value = "show version") // 版本号
    public String v() {
        return "v0.0.1";
    }

    @ShellMethod(key = "help", value = "show help") // help
    public String help() {
        return "commands list: ls,rz,sz,connect,disconnect,esc";
    }

    @ShellMethod(key = "uuid", value = "show local uuid") // uuid
    public String uuid() {
        return UUID.getUUID();
    }

    @ShellMethod(key = "ls", value = "list folder") // ls host
    public String ls(@ShellOption String host) {
        String url = "http://{}:8123/download/ls";
        url = StrUtil.format(url, host);
        String response = HttpUtil.get(url);
        // System.out.println("Response: " + response); 
        return response;
    }

    @ShellMethod(key = "sz", value = "down file to local") // sz host filename
    public void sz(@ShellOption String host,@ShellOption String filename) {
        String url = "http://{}:8123/download/{}";
        url = StrUtil.format(url, host, filename);
        System.out.println(url);
        String spath=Config.GetSavePath()+"\\"+filename;
        System.out.println(spath);
        long size = HttpUtil.downloadFile(url, FileUtil.file(spath));
        System.out.println("文件下载完成，大小: " + size + " bytes");
    }

    @ShellMethod(key = "rz", value = "upload file to remote") // rz host filename
    public void rz(@ShellOption String host,@ShellOption String filename) {
        String url = "http://{}:8123/upload/";
        url = StrUtil.format(url, host);
        System.out.println(url);
        System.out.println(filename);
        filename = filename.replace("\\","\\\\");
        System.out.println(filename);
        HttpResponse response = HttpRequest.post(url)
                        .form("file", new File(filename)) // 添加文件参数
                        .execute();
        System.out.println("上传结果：" + response.body());
    }

    @ShellMethod(key = "exit", value = "disconnect from host") // exit
    public void exit() {
        System.out.println("will exit this app");
        // 调用本地接口，退出程序
        String url = "http://localhost:8123/ctl/shutdown431";
        String response = HttpUtil.get(url);
        // System.out.println("Response: " + response);         
    }

    @ShellMethod(key = "shutdown", value = "shutdown this app") // shutdown
    public void shutdown() {
        System.out.println("will shutdown this app");
        // 调用本地接口，退出程序
        String url = "http://localhost:8123/ctl/shutdown431";
        String response = HttpUtil.get(url);
        // System.out.println("Response: " + response); 
    }

    
}
